<!DOCTYPE HTML>
<html lang="en" class="sidebar-visible no-js light">
    <head>
        <!-- Book generated using mdBook -->
        <meta charset="UTF-8">
        <title>Monolithization - Oatpp教程</title>
        <!-- Custom HTML head -->
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="theme-color" content="#ffffff" />

        <link rel="icon" href="favicon.svg">
        <link rel="shortcut icon" href="favicon.png">
        <link rel="stylesheet" href="css/variables.css">
        <link rel="stylesheet" href="css/general.css">
        <link rel="stylesheet" href="css/chrome.css">
        <link rel="stylesheet" href="css/print.css" media="print">
        <!-- Fonts -->
        <link rel="stylesheet" href="FontAwesome/css/font-awesome.css">
        <link rel="stylesheet" href="fonts/fonts.css">
        <!-- Highlight.js Stylesheets -->
        <link rel="stylesheet" href="highlight.css">
        <link rel="stylesheet" href="tomorrow-night.css">
        <link rel="stylesheet" href="ayu-highlight.css">

        <!-- Custom theme stylesheets -->
    </head>
    <body>
        <!-- Provide site root to javascript -->
        <script>
            var path_to_root = "";
            var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
        </script>

        <!-- Work around some values being stored in localStorage wrapped in quotes -->
        <script>
            try {
                var theme = localStorage.getItem('mdbook-theme');
                var sidebar = localStorage.getItem('mdbook-sidebar');

                if (theme.startsWith('"') && theme.endsWith('"')) {
                    localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
                }

                if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
                    localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
                }
            } catch (e) { }
        </script>

        <!-- Set the theme before any content is loaded, prevents flash -->
        <script>
            var theme;
            try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
            if (theme === null || theme === undefined) { theme = default_theme; }
            var html = document.querySelector('html');
            html.classList.remove('no-js')
            html.classList.remove('light')
            html.classList.add(theme);
            html.classList.add('js');
        </script>

        <!-- Hide / unhide sidebar before it is displayed -->
        <script>
            var html = document.querySelector('html');
            var sidebar = 'hidden';
            if (document.body.clientWidth >= 1080) {
                try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
                sidebar = sidebar || 'visible';
            }
            html.classList.remove('sidebar-visible');
            html.classList.add("sidebar-" + sidebar);
        </script>

        <nav id="sidebar" class="sidebar" aria-label="Table of contents">
            <div class="sidebar-scrollbox">
                <ol class="chapter"><li class="chapter-item expanded "><a href="DTO.html"><strong aria-hidden="true">1.</strong> DTO</a></li><li class="chapter-item expanded "><a href="Async概述.html"><strong aria-hidden="true">2.</strong> Async</a></li><li class="chapter-item expanded "><a href="Simple_vs_Async.html"><strong aria-hidden="true">3.</strong> Simple vs Async</a></li><li class="chapter-item expanded "><a href="协程.html"><strong aria-hidden="true">4.</strong> 协程</a></li><li class="chapter-item expanded "><a href="关系型对象映射框架.html"><strong aria-hidden="true">5.</strong> ORM</a></li><li class="chapter-item expanded "><a href="Monolithization.html" class="active"><strong aria-hidden="true">6.</strong> Monolithization</a></li><li class="chapter-item expanded "><a href="步骤指南.html"><strong aria-hidden="true">7.</strong> 步骤指南</a></li></ol>
            </div>
            <div id="sidebar-resize-handle" class="sidebar-resize-handle"></div>
        </nav>

        <div id="page-wrapper" class="page-wrapper">

            <div class="page">
                <div id="menu-bar-hover-placeholder"></div>
                <div id="menu-bar" class="menu-bar sticky bordered">
                    <div class="left-buttons">
                        <button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
                            <i class="fa fa-bars"></i>
                        </button>
                        <button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
                            <i class="fa fa-paint-brush"></i>
                        </button>
                        <ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
                            <li role="none"><button role="menuitem" class="theme" id="light">Light</button></li>
                            <li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
                            <li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
                            <li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
                            <li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
                        </ul>
                        <button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
                            <i class="fa fa-search"></i>
                        </button>
                    </div>

                    <h1 class="menu-title">Oatpp教程</h1>

                    <div class="right-buttons">
                        <a href="print.html" title="Print this book" aria-label="Print this book">
                            <i id="print-button" class="fa fa-print"></i>
                        </a>
                    </div>
                </div>

                <div id="search-wrapper" class="hidden">
                    <form id="searchbar-outer" class="searchbar-outer">
                        <input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
                    </form>
                    <div id="searchresults-outer" class="searchresults-outer hidden">
                        <div id="searchresults-header" class="searchresults-header"></div>
                        <ul id="searchresults">
                        </ul>
                    </div>
                </div>
                <!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
                <script>
                    document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
                    document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
                    Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
                        link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
                    });
                </script>

                <div id="content" class="content">
                    <main>
                        <h1 id="monolithization"><a class="header" href="#monolithization">Monolithization</a></h1>
<p>Monolithization是一项能让你更好的扩展收缩微型服务器规模的技术。在Oat++中你可以开发一个普通的微型服务器，并且当这个微服务器规模小到在你系统上不会有明显负载时，你可以将这些服务合并为一个monolith。此外，合并服务器可以带来更好的性能并且消耗更少的内存。因此你可以通过降低网络负载和减少运行实例的数量进而减少基础设置的开销。</p>
<p>最好理解Monolithization和它如何工作的方式是通过例子</p>
<h2 id="example"><a class="header" href="#example">Example</a></h2>
<p>考虑以下由三个零散的微服务器组成的系统</p>
<ul>
<li>User Service</li>
<li>Book Service</li>
<li>Facade</li>
</ul>
<p><img src="Monolithization/image.png" alt="" /></p>
<p>Facade请求user信息从User-Service，book信息从Book-Service。然后合并数据并且返回给客户端</p>
<h2 id="问题"><a class="header" href="#问题">问题</a></h2>
<p>当在系统中只有一点负载的时候，就没有必要缩放这个部分。因此一个更好的做法是将这个系统作为一个monolith，而不是安排多个实例或者容器。同时为了保持灵活的特性，你依然需要保持微服务架构。</p>
<p>你也许将所有服务部署在运行于单个处理器的单个实例中。这种情况或许是附加了devops的结果，通过localhost传输数据会降低性能，而且你依然有网络方面的开销。</p>
<h2 id="monolithization的观念"><a class="header" href="#monolithization的观念">Monolithization的观念</a></h2>
<p>这个观念是简单的，我们将会把所有服务运行在单个处理器上，并且这些服务将会通过Oat++虚连接进行通信</p>
<p>使用Oat++虚连接替代网络传输层，因此你的应用程序逻辑可以独立出来，并且他们利用内部缓冲而不是通过localhost通信，因此没有网络方面的开销</p>
<h3 id="服务接口"><a class="header" href="#服务接口">服务接口</a></h3>
<p>一致的服务可以保持他们的网络接口，并且对远程服务器是可用的。并且他们通过统一的服务通过虚连接暴露一致的接口</p>
<h3 id="构建管道pipeline"><a class="header" href="#构建管道pipeline">构建管道(pipeline)</a></h3>
<p>一个好事情是你不需要改变你构建的管道，每一个团队都可以独立的开发他们自己的服务器。</p>
<p>如果你需要统一这些服务，你需要为每一个monolithic创建一份配置</p>
<h3 id="缩放"><a class="header" href="#缩放">缩放</a></h3>
<p>通过Monolithization，你可以渐进式缩放服务，一步一步添加新的monolithic部署，并且去耦合成更小的快，最终使你的云服务器达到最佳的配置。</p>
<h2 id="性能"><a class="header" href="#性能">性能</a></h2>
<p>以下是性能测试，通过对比Oat++微服务(部署在相同的实例上)和通过Monolithization合并的同样的微服务器性能</p>
<blockquote>
<p>这里原先有图的，现在不知道为啥图打不开了</p>
</blockquote>
<h3 id="请求时间"><a class="header" href="#请求时间">请求时间</a></h3>
<blockquote>
<p>图丢了</p>
</blockquote>
<h3 id="内存占用mb"><a class="header" href="#内存占用mb">内存占用(MB)</a></h3>
<div class="table-wrapper"><table><thead><tr><th></th><th>Facade</th><th>User Service</th><th>Book Service</th><th>Total</th></tr></thead><tbody>
<tr><td>Oat++ (micro-svc) idle</td><td>25</td><td>3</td><td>3</td><td>31</td></tr>
<tr><td>Oat++ (micro-svc) load</td><td>45</td><td>3.2</td><td>3.2</td><td>49.4</td></tr>
<tr><td>Oat++ (monolith) idle</td><td>-</td><td>-</td><td>-</td><td>26.7</td></tr>
<tr><td>Oat++ (monolith) load</td><td>-</td><td>-</td><td>-</td><td>37.7</td></tr>
</tbody></table>
</div>
                    </main>

                    <nav class="nav-wrapper" aria-label="Page navigation">
                        <!-- Mobile navigation buttons -->
                            <a rel="prev" href="关系型对象映射框架.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
                                <i class="fa fa-angle-left"></i>
                            </a>
                            <a rel="next" href="步骤指南.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
                                <i class="fa fa-angle-right"></i>
                            </a>
                        <div style="clear: both"></div>
                    </nav>
                </div>
            </div>

            <nav class="nav-wide-wrapper" aria-label="Page navigation">
                    <a rel="prev" href="关系型对象映射框架.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
                        <i class="fa fa-angle-left"></i>
                    </a>
                    <a rel="next" href="步骤指南.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
                        <i class="fa fa-angle-right"></i>
                    </a>
            </nav>

        </div>

        <!-- Livereload script (if served using the cli tool) -->
        <script>
            const wsProtocol = location.protocol === 'https:' ? 'wss:' : 'ws:';
            const wsAddress = wsProtocol + "//" + location.host + "/" + "__livereload";
            const socket = new WebSocket(wsAddress);
            socket.onmessage = function (event) {
                if (event.data === "reload") {
                    socket.close();
                    location.reload();
                }
            };

            window.onbeforeunload = function() {
                socket.close();
            }
        </script>
        <script>
            window.playground_copyable = true;
        </script>
        <script src="elasticlunr.min.js"></script>
        <script src="mark.min.js"></script>
        <script src="searcher.js"></script>
        <script src="clipboard.min.js"></script>
        <script src="highlight.js"></script>
        <script src="book.js"></script>

        <!-- Custom JS scripts -->
    </body>
</html>
